package com.bbm.bbmanage.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bbm.bbmanage.entity.AppHttpCodeEnum;
import com.bbm.bbmanage.entity.Category;
import com.bbm.bbmanage.entity.vo.LabelValueVo;
import com.bbm.bbmanage.entity.vo.ResponseResult;
import com.bbm.bbmanage.entity.vo.CategoryVo;
import com.bbm.bbmanage.mapper.CategoryMapper;
import com.bbm.bbmanage.service.CategoryService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Category)表服务实现类
 *
 * @author lionkliu
 * @since 2023-12-06 09:31:36
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        return ResponseResult.okResult(page(new Page<>(pageNum, pageSize)));
    }

    @Cacheable
    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(Category category) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
        String number = sdf.format(System.currentTimeMillis());//202211041552160219
        //补充一个三位随机数
        int x = (int) (Math.random() * 900) + 100;
        String serialNumber = number + x;
        Category cate = new Category();
        cate.setKey(serialNumber);
        cate.setName(category.getName());
        cate.setIcon(category.getIcon());
        return save(cate) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(Category category) {
        LambdaUpdateWrapper<Category> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(Category::getId, category.getId());
        updateWrapper.set(Category::getName, category.getName());
        return update(null, updateWrapper) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }

    @Override
    public ResponseResult getCategory() {
        List<CategoryVo> collect = list().stream().map((category -> {
            CategoryVo categoryVo = new CategoryVo();
            categoryVo.setId(category.getId());
            categoryVo.setLabel(category.getName());
            categoryVo.setKey(category.getKey());
            categoryVo.setChildren(null);
            return categoryVo;
        })).collect(Collectors.toList());
        return ResponseResult.okResult(collect);
    }

    @Override
    public ResponseResult getCategoryLabelValue() {
        List<LabelValueVo> collect = list().stream().map((category -> {
            LabelValueVo categoryVo = new LabelValueVo();
            categoryVo.setValue(String.valueOf(category.getId()));
            categoryVo.setLabel(category.getName());
            return categoryVo;
        })).collect(Collectors.toList());
        return ResponseResult.okResult(collect);
    }

}

